package com.beemans.common.app.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.View
import com.beemans.common.app.R
import com.beemans.common.app.bridge.request.HttpViewModel
import com.beemans.common.app.databinding.FragmentHttpBinding
import com.beemans.common.app.ui.base.BaseFragment
import com.beemans.common.ext.lazyViewModel
import com.beemans.common.ext.setPageBack
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.addObserve
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2021/5/28.
 */
class HttpFragment : BaseFragment() {
    private val dataBinding: FragmentHttpBinding? by lazyDataBindingOrNull { }
    private val viewModel: HttpViewModel by lazyViewModel()

    private var title: String? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_http)
    }

    override fun initParameters(bundle: Bundle?) {
        title = getString(MainFragment.PARAM_TITLE)
    }

    override fun initView(rootView: View?) {
        dataBinding?.httpTitleBar?.setTvTitle { text = title }
    }

    override fun initEvent() {
        dataBinding?.httpTitleBar?.setPageBack()

        dataBinding?.httpTvGet?.clickNoRepeat {
            get()
        }

        dataBinding?.httpTvPost?.clickNoRepeat {
            post()
        }
    }

    override fun createObserver() {
        addObserve(viewModel.resultLiveData) {
            Log.e("susu", "response:$it")
            val msg = if (it == null) "请求失败" else "请求成功"
            showToast(msg)
        }
    }

    private fun get() {
        viewModel.get()
    }

    private fun post() {
        viewModel.post()
    }

    override fun doBusiness() {
    }
}